#include "id3TagParser.h"

/*int main(int argc,char **argv)
{
	char tempBuff[1024] = {'\0'};
	getInfo(argv[1],tempBuff);
	cout<<tempBuff<<endl;
	return 0;
}*/

extern "C" void getInfo(char *filename, id3filedata *mp3data)
{
	ID3_Tag myFile;
	ID3_Frame *myFrame;
	int i;
	char tempBuff[512];
	myFile.Link(filename);

	for (i = 0; i < 6; i++)
	{
		memset(tempBuff,0,512);
		if (i == 0)
		{
			myFrame = myFile.Find(ID3FID_YEAR);
			if (myFrame != NULL)
			{
				(myFrame->GetField(ID3FN_TEXT))->Get(tempBuff, 1024);
				mp3data->year = atoi(tempBuff);
			}
			else
			{
				mp3data->year = 0;
			}
		}
		else if (i == 1)
		{
			myFrame = myFile.Find(ID3FID_TITLE);
			if (myFrame != NULL)
			{
				(myFrame->GetField(ID3FN_TEXT))->Get(tempBuff, 1024);
				mp3data->title = strdup(tempBuff);
			}
			else
			{
				mp3data->title = strdup("Unknown");
			}

		}
		else if (i == 2)
		{
			myFrame = myFile.Find(ID3FID_ALBUM);
			if (myFrame != NULL)
			{
				(myFrame->GetField(ID3FN_TEXT))->Get(tempBuff, 1024);
				mp3data->album = strdup(tempBuff);
			}
			else
			{
				mp3data->album = strdup("Unknown");
			}
		}
		else if (i == 3)
		{
			myFrame = myFile.Find(ID3FID_LEADARTIST);
			if (myFrame != NULL)
			{
				(myFrame->GetField(ID3FN_TEXT))->Get(tempBuff, 1024);
				mp3data->artist = strdup(tempBuff);
			}
			else
			{
				mp3data->artist = strdup("Unknown");
			}
		}
		else if (i == 4)
		{
			mp3data->path = strdup(filename);
		}
		else if (i == 5)
		{
			char *temp = strrchr(filename,'/');
			mp3data->filename = strdup(temp+1);
		}
	}
}

extern "C" void getInfostr(char *filename,char *fileInfo)
{
	ID3_Tag myFile;
	ID3_Frame *myFrame;
	int i;
	char tempBuff[512];
	myFile.Link(filename);

	tempBuff[0] = '\0';
	for (i = 0; i < 5; i++)
	{
		if (i == 0)
		{
			myFrame = myFile.Find(ID3FID_YEAR);
	//		strcat(fileInfo,"|Year:");
		}
		else if (i == 1)
		{
			myFrame = myFile.Find(ID3FID_TITLE);
			strcat(fileInfo,"|");
		}		
		else if (i == 2)
		{
			myFrame = myFile.Find(ID3FID_ALBUM);
			strcat(fileInfo,"|");
		}
		else if (i == 3)
		{
			myFrame = myFile.Find(ID3FID_LEADARTIST  );
			strcat(fileInfo,"|");
		}
		else if (i == 4)
		{
			myFrame = myFile.Find(ID3FID_ORIGFILENAME  );
			strcat(fileInfo,"|");
		}
		else if(i==5)
		{
			strcat(fileInfo,"|");
			strcat(fileInfo,filename);
		}
		
		if ((myFrame != NULL) && (i!=5))
		{
			(myFrame->GetField(ID3FN_TEXT))->Get(tempBuff, 1024);
			strcat(fileInfo,tempBuff);
		}
	}
}


extern "C" void initid3Struct(id3filedata *id3data)
{
	id3data->filename = NULL;
	id3data->path = NULL;
	id3data->artist = NULL;
	id3data->album = NULL;
	id3data->title = NULL;
	id3data->year = 0;
}

extern "C" void freeid3Struct(id3filedata *id3data)
{
	free(id3data->filename);
	free(id3data->path);
	free(id3data->artist);
	free(id3data->album);
	free(id3data->title);
	id3data->year = 0;
}


extern "C" void printData(id3filedata *mp3data)
{
	printf("\n%s: (%d) %s - %s [%s]",mp3data->filename,mp3data->year,mp3data->artist, mp3data->title,mp3data->album);
}
